package gen.tools;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class JobDepStruc {

	private static boolean isContain = true;
	public JobDepStruc(String modId,boolean isMR, String subsysId) {
		this.modId = modId;
		this.isMR = isMR;
		this.subsysId = subsysId;
	}
	
	private boolean isMR = true;
	private String subsysId;
	private JobDepStruc up = null;
	private String modId;
	private ArrayList<JobDepStruc> down =null;
	private int nextBuildNumber;
	private boolean isLeaf = false;
	
	
	public boolean isLeaf() {
		return isLeaf;
	}
	public void setLeaf(boolean isLeaf) {
		this.isLeaf = isLeaf;
	}
	
	
	public static boolean isContain() {
		return isContain;
	}
	public static void setContain(boolean isContain) {
		JobDepStruc.isContain = isContain;
	}
	public int getNextBuildNumber() {
		return nextBuildNumber;
	}
	public void setNextBuildNumber(int nextBuildNumber) {
		this.nextBuildNumber = nextBuildNumber;
	}
	public JobDepStruc getUp() {
		return up;
	}
	public void setUp(JobDepStruc up) {
		this.up = up;
	}
	public ArrayList<JobDepStruc> getDown() {
		return down;
	}
	public void setDown(ArrayList<JobDepStruc> down) {
		this.down = down;
	}
	public String getModId() {
		return modId;
	}
	public void setModId(String modId) {
		this.modId = modId;
	}
	/** 
	 * @param obj
	 * @param modId
	 * @return
	 */
	public boolean isContain(JobDepStruc obj,String modId) {
		System.out.println("modId："+modId);
		System.out.println("getModId()："+getModId());
		
		if ( modId.equalsIgnoreCase( getModId() ) ) {
			System.out.println("進入了IF");
			return true;
		} else {
			System.out.println("進入了else");
			for(int i=0; i<obj.getDown().size();i++){
				isContain = obj.getDown().get(i).isContain(obj.getDown().get(i),modId);
				if(isContain) { return true; }
			}
		}
		return false;
	}
	
	
	/**
	 * 	取得這個模組一下次jenkins執行的job號
	 *	@return
	 *	@throws IOException	
	 *	<br>創建時間：2013/11/22 上午11:02:40<br>
	 *	創建人員：郭啟揚 (842297)
	 */
	public int nextBuildNumber() throws IOException {
		final Widgets wid = new Widgets();
		int nextBuildNumber = 0;
		final String jobName = (isMR?"MR":"OL") + "_" + subsysId +"_"+ this.getModId() ;
		String urlStr = wid.JENKINSURL+"job/"+jobName+"/api/xml";
		// 連線取得資料
        InputStream ins = null; 
        final SAXReader reader = new SAXReader();
        final Map<String,Object> map = new HashMap<String,Object>();
        try {
        	ins = URLConnectionUtil.doGet(urlStr,false,wid.JENKINS_USR,wid.JENKINS_PWD,map);
        	final Document document = reader.read(ins);
    		final Element root = document.getRootElement();
    		final Iterator<Element> it = root.elementIterator();	
        	while (it.hasNext()) {
    			final Element e2 = it.next();
    			if ( "nextBuildNumber".equalsIgnoreCase(e2.getName() ) ) {
    				nextBuildNumber = Integer.parseInt(e2.getText());
    			}
    		}
        } catch (Exception e) {
        	e.printStackTrace();
        } finally {
        	ins.close();
        }
        
        return nextBuildNumber;
	}
	public boolean isMR() {
		return isMR;
	}
	public void setMR(boolean isMR) {
		this.isMR = isMR;
	}
	public String getSubsysId() {
		return subsysId;
	}
	public void setSubsysId(String subsysId) {
		this.subsysId = subsysId;
	}
}
